rust-cpython
Rust bindings for the python interpreter.
- Documentation
- Cargo package: cpython
Copyright (c) 2015-2016 Daniel Grunwald. Rust-cpython is licensed under the MIT license. Python is licensed under the Python License.
Supported Python versions:
- Python 2.7
- Python 3.3
- Python 3.4
- Python 3.5
Supported Rust version:
- Rust 1.13.0 or later
- On Windows, we require rustc 1.15.0-nightly
Usage
To use cpython
, add this to your Cargo.toml
:
[]
= "0.1"
Example program displaying the value of sys.version
:
extern crate cpython;
use ;
Example library with python bindings:
The following two files will build with cargo build
, and will generate a python-compatible library.
On Mac OS, you will need to rename the output from *.dynlib to *.so.
On Windows, you will need to rename the output from *.dll to *.pyd.
Cargo.toml
:
[]
= "rust2py"
= ["cdylib"]
[]
= "../.."
= ["extension-module"]
src/lib.rs
extern crate cpython;
use ;
// add bindings to the generated python module
// N.B: names: "librust2py" must be the name of the `.so` or `.pyd` file
py_module_initializer!;
// logic implemented as a normal rust function
// rust-cpython aware function. All of our python interface could be
// declared in a separate module.
// Note that the py_fn!() macro automatically converts the arguments from
// Python objects to Rust values; and the Rust return value back into a Python object.